package com.gnerv.battle.boot.module.base.exception;

import com.gnerv.battle.boot.common.base.exception.BusinessException;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.base.model.RestResultCode;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常处理
     */
    @ExceptionHandler(value = BusinessException.class)
    public RestResult<Void> businessExceptionHandler(HttpServletRequest req, Exception e) {
        log.error(e.getMessage(), e);
        return RestResult.error(RestResultCode.CODE_406);
    }

    /**
     * 通用异常处理
     */
    @ExceptionHandler(value = Exception.class)
    public RestResult<Void> defaultErrorHandler(HttpServletRequest req, Exception e) {
        log.error(e.getMessage(), e);
        return RestResult.error(RestResultCode.CODE_400);
    }

}

